<template>
  <div>
    <MyTable :arr='list'>
      <!-- 插槽的用法template #代替v-slot:-->
      <template #head>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <!-- 插槽里面传过来的值可以解构 -->
      <template #body='{row}'>
        <td>{{ row.id }}</td>
        <td>{{ row.goods_name }}</td>
        <td>{{ row.goods_price }}</td>
        <td>

          <input
          class="tag-input form-control"
          style="width: 100px;"
          type="text"
          v-if="row.inputVisible" 
          v-fff
          v-model="row.inputValue"
          @blur='row.inputVisible=false'
          @keydown.esc="row.inputValue=''"
          @keydown.enter="enterFn(row)"
          />
          <button 
          v-else 
          style="display: block;" 
          class="btn btn-primary btn-sm add-tag"
          @click="row.inputVisible = true"
          >+Tag</button>


          <span v-for="(str, index) in row.tags"
          :key='index'
          class='badge badge-warning'
          >{{ str }}</span>
        </td>
        <td>
          <button @click='delFn(row.id)' class="btn btn-danger btn-sm">删除</button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from "../components/MyTable";

export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [] // 所有数据
    };
  },
  //axios请求
  created() {
    this.$axios({
      url:'/api/goods'
    }).then(res => {
      this.list = res.data.data //把数组传给list
    }).catch(err => {
      log.error(err);
    })
  },
  methods:{
    // 删除功能,filter筛选数据
    delFn(id){
      this.list = this.list.filter(item => item.id !== id)
    },
    enterFn(row){
      if(row.inputValue.trim() ===``){
        return alert('可能你也有烦恼吧QAQ')
      }
      row.tags.push(row.inputValue)
      row.inputValue = ``
    }
  },
  //自定义功能 自动焦点功能
  directives: {
    fff:{
      inserted(el) {
        el.focus()
      }
    }
  }
};
</script>